package com.lxl.bt.view.base;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;


import androidx.annotation.FloatRange;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.lxl.bt.R;
import com.lxl.bt.bean.EventBusBean;
import com.lxl.bt.util.ScreenUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * created by zzj
 * on 2019/5/31
 */
public abstract class BaseDialogFragment extends DialogFragment {

    @LayoutRes
    protected int mLayoutResId;

    private View view;
    private Unbinder unbinder;
    protected Context mContext;
    private int mWidth;//对话框宽度
    private int mHeight;//对话框高度
    private int mMargin = 0;//左右边距
    private int mTopMargin=0;
    private float mDimAmount = 0.5f;//背景昏暗度
    private int gravity = Gravity.CENTER;
    private int mAnimStyle = 0;//进入退出动画
    private boolean mOutCancel = true;//点击外部取消
    private boolean isShowing;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogStyle);
        mLayoutResId = setUpLayoutId();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(mLayoutResId, null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        initParams();
    }

    private void initParams() {
        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.dimAmount = mDimAmount;
            //设置dialog显示位置
            params.gravity = gravity;
            //设置dialog宽度
            if (mWidth == 0) {
                params.width = ScreenUtils.getScreenWidth(mContext) - 2 * ScreenUtils.dip2px(mContext, mMargin);
            } else {
                params.width = ScreenUtils.dip2px(mContext, mWidth);
            } //设置dialog高度
            if (mHeight == 0) {
                params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            } else if (mHeight == -1) {
                params.height = WindowManager.LayoutParams.MATCH_PARENT;
            } else {
                params.height = ScreenUtils.dip2px(mContext, mHeight);
            }
            //设置dialog动画
            if (mAnimStyle != 0) {
                window.setWindowAnimations(mAnimStyle);
            }
            window.setAttributes(params);
        }
        setCancelable(mOutCancel);
    }

    /**
     * 设置背景昏暗度
     *
     * @param dimAmount
     * @return
     */
    public BaseDialogFragment setDimAmout(@FloatRange(from = 0, to = 1) float dimAmount) {
        mDimAmount = dimAmount;
        return this;
    }


    /**
     * 显示位置，默认中间
     *
     * @param gravity
     * @return
     */
    public BaseDialogFragment showGravity(int gravity) {
        this.gravity = gravity;
        return this;
    }

    /**
     * 设置宽高
     *
     * @param width
     * @param height
     * @return
     */
    public BaseDialogFragment setSize(int width, int height) {
        mWidth = width;
        mHeight = height;
        return this;
    }

    /**
     * 设置左右margin
     *
     * @param margin
     * @return
     */
    public BaseDialogFragment setMargin(int margin) {
        mMargin = margin;
        return this;
    }

    /**
     * 设置进入退出动画
     *
     * @param animStyle
     * @return
     */
    public BaseDialogFragment setAnimStyle(@StyleRes int animStyle) {
        mAnimStyle = animStyle;
        return this;
    }

    /**
     * 设置是否点击外部取消
     *
     * @param outCancel
     * @return
     */
    public BaseDialogFragment setOutCancel(boolean outCancel) {
        mOutCancel = outCancel;
        return this;
    }

    /**
     * 设置dialog布局
     *
     * @return
     */
    public abstract int setUpLayoutId();

    public abstract void initView();

    @Override
    public void show(FragmentManager manager, String tag) {
        try {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commitAllowingStateLoss();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    public BaseDialogFragment show(FragmentManager manager) {
        if (!isAdded()) {
            show(manager, getClass().getSimpleName());
        }
        isShowing = true;
        return this;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        isShowing = false;
        if (dismissListener != null) {
            dismissListener.dismiss();
        }
    }

    public boolean isShowing() {
        return isShowing;
    }

    public interface OnDismissListener {
        void dismiss();
    }

    OnDismissListener dismissListener;

    public void setOnDismissListener(OnDismissListener listener) {
        this.dismissListener = listener;
    }

    @Override
    public void onDestroyView() {
        if (unbinder != null) {
            unbinder.unbind();
        }
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getEventData(EventBusBean eventDataBen) {
        onEventBusData(eventDataBen);
    }

    /**
     * 接收eventbus数据
     *
     * @param eventDataBen
     */
    protected void onEventBusData(EventBusBean eventDataBen) {

    }
}
